package com.kuang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuang.pojo.Product;
import com.kuang.pojo.Return;
import com.kuang.pojo.Sale;
import com.kuang.service.ReturnServiceImpl;
import com.kuang.service.SaleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.kuang.service.ProductServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/return")
public class ReturnController {
    @Autowired
    @Qualifier("resturnservice")
    private ReturnServiceImpl returnService;
    @Autowired
    private SaleServiceImpl saleService;
 @Autowired
 private ProductServiceImpl productService;
    @RequestMapping("/getReturn")
    public String getReturn(@RequestParam(required=true,value="pageNum",defaultValue="1") Integer pageNum, @RequestParam(required=true,value="pageSize",defaultValue="6")Integer pageSize, Model model){
        PageHelper.startPage(pageNum,pageSize);
//        List<Return> returns = new ArrayList<>();
        List<Return> returnList = returnService.getReturn();
//        for (int i=0;i<returnList.size();i++){
//            if (returnList.get(i).getFlag()==0){
//                returns.add(returnList.get(i));
//            }
//        }
        //System.out.println(returns);
        PageInfo<Return> returnPageInfo=new PageInfo<>(returnList);
        System.out.println("===============================");

        if (pageNum==0 ||pageNum>returnPageInfo.getPages()){

            return "redirect:/return/getReturn";

        }
        model.addAttribute("returnPageInfo",returnPageInfo);
        return "return";
    }
    //需要审核的退货信息
    @RequestMapping("/getReturn02")
    public String getReturn02(@RequestParam(required=true,value="pageNum",defaultValue="1") Integer pageNum, @RequestParam(required=true,value="pageSize",defaultValue="6")Integer pageSize, Model model){
        PageHelper.startPage(pageNum,pageSize);
         List<Return> returns = new ArrayList<>();

        List<Return> returnList = returnService.getReturn();
        for (int i=0;i<returnList.size();i++){
            if (returnList.get(i).getFlag()==0){
                returns.add(returnList.get(i));
            }
        }
        //如果没有需要审核的信息
        if (returns.size()==0){
            model.addAttribute("noreviewinformation","没有需要审核的信息");
            return "/putong/return";
        }
        System.out.println(returns);
        PageInfo<Return> returnPageInfo=new PageInfo<>(returns);
        System.out.println("===============================");

        if (pageNum==0 ||pageNum>returnPageInfo.getPages()){

            return "redirect:/return/getReturn02";

        }
        model.addAttribute("returnPageInfo",returnPageInfo);
        return "/putong/return";
    }
    //跳转到增加页面 ================================
    @RequestMapping("/redirectadd")
    public String redirectadd(Model model){
        List<Sale> sale = saleService.getSale();
  model.addAttribute("saleproductname",sale);
        return "addreturn";
    }
//    //跳转到增加页面
//    @RequestMapping("/redirectadd02")
//    public String redirectadd02(){
//        return "/putong/addreturn";
//    }
    //增加信息=================================
    @RequestMapping("/addreturn")
    public String addreturn(Return re, Model model, HttpServletRequest request){
        //获取商品的name
        String name = re.getName();
        //获取 商品的价格
        int productidbyname = productService.productidbyname(name);
        Product productById = productService.getProductById(productidbyname);
        String price = productById.getPrice();
        //获取商品的数量
        int number = re.getNumber();

        //商品的原因
        String reason = re.getReason();
        //获取退货的日期
        String date = re.getDate();
        //商品的供货商
        String suppiler = productById.getSuppiler();
        //商品的备注
        String detail = re.getDetail();
        //顾客联系电话
        String cphone = re.getCphone();
        HttpSession session = request.getSession();
        //根据商品的id去查找商品的销售信息
        String s = Integer.toString(productidbyname);
        Sale saleById = saleService.getSaleById(s);
        String number1 = saleById.getNumber();
        int i = Integer.parseInt(number1);
        if (number>i){
            //model.addAttribute("addreturnerror","输入退货数量出错");

            session.setAttribute("adderror","输入退货数量出错");
            return "redirect:/return/redirectadd";
        }
        returnService.addReturn(new Return(name,price,number,reason,suppiler,date,detail,cphone,0));

        return "redirect:/return/getReturn";
    }
//    //增加信息
//    @RequestMapping("/addreturn02")
//    public String addreturn02(Return re){
//        returnService.addReturn(re);
//        return "redirect:/return/getReturn02";
//    }
    //根据id查找信息
    @RequestMapping("redirectupdate")
    public String  getReturnById(int id,Model model,HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("updatereturn");

      //  session.setAttribute("updatereturn","输入的数量出错");

        Return returnById = returnService.getReturnById(id);
        int number = returnById.getNumber();
        session.setAttribute("number",number);
        model.addAttribute("returnById",returnById);
        return "updatereturn";
    }
//    //根据id查找信息
//    @RequestMapping("redirectupdate03")
//    public String  getReturnById03(int id,Model model){
//        Return returnById = returnService.getReturnById(id);
//        model.addAttribute("returnById",returnById);
//        return "/putong/updatereturn";
//    }
    //修改信息
    //============================================修改信息==========================================
    @RequestMapping("/update")
    public String updateReturn(Return re,HttpServletRequest request){
        returnService.updateReturn(re);

        return "redirect:/return/getReturn";




    }
    //根据id查找信息
    @RequestMapping("/redirectupdate05")
    public String  getReturnById05(int id,Model model){
        Return returnById = returnService.getReturnById(id);
        model.addAttribute("returnById",returnById);
        System.out.println("1111111111111111111111");
        return "updatereturn";
    }
    //修改信息
    @RequestMapping("/update02")
    public String updateReturn02(Return re){
        returnService.updateReturn(re);
        return "redirect:/return/getReturn02";
    }
@RequestMapping("deletereturn")
 public String deleteReturn(int id){
        returnService.deleteReturn(id);
    return "redirect:/return/getReturn";
}
    @RequestMapping("deletereturn02")
    public String deleteReturn02(int id){
        returnService.deleteReturn(id);
        return "redirect:/return/getReturn02";
    }
//根据id查找信息
    @RequestMapping("/getReturnById")
    public String getReturnById02(int id,Model model){
        Return returnById = returnService.getReturnById(id);
        model.addAttribute("product",returnById);
        return "byidreturn";
    }
    //根据id查找信息
    @RequestMapping("/getReturnById04")
    public String getReturnById04(int id,Model model){
        Return returnById = returnService.getReturnById(id);
        model.addAttribute("product",returnById);
        return "putong/byidreturn";
    }

//审核通过
    @RequestMapping("/returnGoods")
    public String returnGoods(HttpServletRequest request){
           //
        String id = request.getParameter("id");
//        System.out.println(id);
        Return re = returnService.getReturnById(Integer.parseInt(id));
        for (Sale sale:saleService.getSqlByname(re.getName())){
            if (saleService.getSqlByname(re.getName()).size()==1){
                Double num = Double.parseDouble(sale.getNumber())-re.getNumber();
                sale.setNumber(num.toString());
                saleService.updateSale(sale);
            }
        }
        for (Product product:productService.queryProductByName(re.getName())){
            if (productService.queryProductByName(re.getName()).size()==1){
                Double num = re.getNumber()+Double.parseDouble(product.getNumber());
                product.setNumber(num.toString());
                productService.updateById(product);
            }
        }
        re.setFlag(1);
        returnService.updateflagbyid(re);
        return "redirect:/return/getReturn02";
    }

}
